<?php
namespace App\Services;
use App\Models\Comment;
use App\Models\Share;
use App\Models\Star;

class UserWeightService {

   public static function handle($user, $start_at) {
       $weight = 0;

       $star_num = Star::where('entity_user_id', $user->id)->count();
       if ($star_num)
           $weight += $star_num;

       $share_num = Share::where('entity_user_id', $user->id)->where('user_id', '!=', $user->id)->count();
       if ($share_num)
           $weight += $share_num * 13;

       $comment_length_1 = Comment::where('entity_user_id', $user->id)->where('created_at', '>=', $start_at)->where('comment_string_length', '>=', 15)->exists();
       if ($comment_length_1)
           $weight +=  13;

       $comment_length_2 = Comment::where('entity_user_id', $user->id)->where('created_at', '>=', $start_at)->where('comment_string_length', '<', 15)->exists();
       if ($comment_length_2)
           $weight +=  6;

       $hour = floor((time()-strtotime($user->created_at))%86400/3600);
       if ($hour <= 72) {
           $weight += 4;
       }

       $fans_count = $user->fans_count;
       if ($fans_count > 10 && $fans_count <= 50)
           $weight += 2;

       if ($fans_count > 50 && $fans_count <= 100)
           $weight += 4;

       if ($fans_count > 100)
           $weight += 6;

       $user->hot_search_weight = $weight;
       $user->save();
   }

}
